package com.zytech.zspider.task.impl;

import com.zytech.modules.script.ScriptEvaluator;
import com.zytech.modules.script.ScriptResult;
import com.zytech.modules.script.groovy.GroovyScriptEvaluator;
import com.zytech.zspider.task.TaskContext;
import com.zytech.zspider.task.TaskExpressionEvaluator;

/**
 * @author siuming
 */
public class TaskExpressionEvaluatorImpl implements TaskExpressionEvaluator {

    private final ScriptEvaluator evaluator;

    /**
     * @param externalScripts
     */
    public TaskExpressionEvaluatorImpl(String[] externalScripts) {
        this.evaluator = GroovyScriptEvaluator.of(externalScripts);
    }

    @Override
    public Object eval(String expr, TaskContext context) {
        ScriptResult result = evaluator.eval(expr, context.getAllBindings());
        context.putAllBindings(result.getUserVarBindings());
        return result.getValue();
    }
}
